#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../inc/student.h"
#include "../inc/admin.h"
#include "../inc/general.h"

// 打印主菜单函数
void printf_function_menu(int login_flag)
{
    if (login_flag == 10)
    {
        printf("                 学生管理系统           \n");
    }
    else
    {
        printf("             学生管理系统-管理端          \n");
    }
    print_Lines();
    printf("| 1、录入学生信息\t\t             |\n");
    printf("| 2、删除学生信息\t\t             |\n");
    printf("| 3、修改学生信息\t\t             |\n");
    printf("| 4、查看学生信息\t\t             |\n");
    if (login_flag == 10)
    {
        printf("| 5、退出登录\t\t\t             |\n");
    }
    else if (login_flag == 11)
    {
        printf("| 5、查看账号信息\t\t             |\n");
        printf("| 6、账号权限管理\t\t             |\n");
        printf("| 7、退出登录\t\t\t             |\n");
    }
    printf("| 0、退出系统\t\t\t             |\n");
    print_Lines();
    printf("请输入相应的序号选择:");
}

// 执行菜单
int menuOperate(int menu, int login_flag, Student_Node_p stu_head, Admin_Node_p admin_head)
{
    while (getchar() != '\n')
        ;
    clearScreen();
    switch (menu)
    {
    case ADD_STUDENT:
        add_student(stu_head);
        return_Main();
        break;
    case DELETE_STUDENT:
        delete_student(stu_head);
        break;
    case UPDATE_STUDENT:
        updata_student(stu_head);
        break;
    case VIEW_STUDENT:
        show_student(stu_head);
        break;
    case VIEW_ALL_ADMINS:
        if (login_flag == 10)
        { // 退出登录
            return 0;
            break;
        }
        show_allAdmin(admin_head);
        clearScreen();
        break;
    case GIVE_PRIVILEAGE:
        if (login_flag == 10)
        {
            break;
        }
        give_privilege(admin_head);
        break;
    case LOGOUT:
        return 0;
        break;
    case EXIT_SYSTEM:
        printf("退出成功!");
        exit(0);
    default:
        break;
    }
    return login_flag;
}

// 录入学生信息
void add_student(Student_Node_p stu_head)
{
    printf("                 录入学生信息                  \n");
    print_Lines();
    printf("开始录入学生信息:\n");
    insertTail_studentNode(stu_head);
    printf("录入成功!录入信息:\n");
    print_student(find_studentNode(stu_head, stuCount(stu_head) - 1));
}

// 删除学生信息
void delete_student(Student_Node_p stu_head)
{
    printf("                  删除学生信息                 \n");
    print_Lines();
    printf("请输入要删除的学生学号：");
    char id[20];
    scanf("%19s", id);
    int index = getStudentIndex_ByID(stu_head, id);

    if (index != -1)
    {
        Student_Node_p stuNode = find_studentNode(stu_head, index);
        Student_Node_p temp = stuNode;
        delete_studentNode(stu_head, index);
        printf("删除成功!删除学生信息为:");
        print_student(temp);
        return_Main();
    }
    else
    {
        printf("不存在该学号的学生!\n");
        if (return_function() == 1)
        {
            delete_student(stu_head);
        }
        else
        {
            return;
        }
    }
}

// 修改学生信息
void updata_student(Student_Node_p stu_head)
{
    printf("                  修改学生信息                 \n");
    print_Lines();
    printf("请输入要修改的学生学号：");
    char id[20];
    scanf("%19s", id);
    int index = getStudentIndex_ByID(stu_head, id);
    if (index != -1)
    {
        Student_Node_p currNode = updata_studentNode(stu_head, index);
        printf("修改成功!修改学生信息为:");
        print_student(currNode);
        return_Main();
    }
    else
    {
        printf("不存在该学号的学生!\n");
        if (return_function() == 1)
        {
            updata_student(stu_head);
        }
        else
        {
            return;
        }
    }
}

// 打印学生信息
void print_student(Student_Node_p stuNode)
{
    printf("学号:%s,姓名:%s,班级:%s,年龄:%d,性别:%s\n", stuNode->data->id, stuNode->data->name, stuNode->data->class, stuNode->data->age, getSex(stuNode->data->sex));
}

// 选择查看方式
void show_student(Student_Node_p stu_head)
{
    while (1)
    {
        clearScreen();
        printf("                  选择查看方式                 \n");
        print_Lines();
        printf("| 1、查找单个学生\t\t             |\n");
        printf("| 2、查找班级学生\t\t             |\n");
        printf("| 3、查找全部学生\t\t             |\n");
        print_Lines();
        printf("指令: (1-3)查找方式 (Q)返回上一级\n");
        printf("请输入相应的指令选择:");
        char choose;
        scanf("%c", &choose);
        clearScreen();
        switch (choose)
        {
        case '1':
            show_one(stu_head);
            break;
        case '2':
            show_class(stu_head);
            break;
        case '3':
            show_all(stu_head);
            break;
        case 'Q':
        case 'q':
        case '4':
            return;
        default:
            printf("输入错误!\n");
            break;
        }
    }
}

// 查看学生信息
void show_one(Student_Node_p stu_head)
{
    printf("                  查看学生信息                 \n");
    print_Lines();
    printf("请输入查看方式(1.按学号 2.按姓名):");
    int choose;
    scanf("%d", &choose);
    if (choose == 1)
    {
        printf("请输入要查看的学生学号：");
        char id[20];
        scanf("%19s", id);
        int index = getStudentIndex_ByID(stu_head, id);
        if (index != -1)
        {
            Student_Node_p currNode = find_studentNode(stu_head, index);
            printf("查询成功!查询学生信息为:\n");
            print_student(currNode);
            return_Main();
        }
        else
        {
            printf("不存在该学号的学生!\n");
            if (return_function() == 1)
            {
                delete_student(stu_head);
            }
            else
            {
                return;
            }
        }
    }
    else if (choose == 2)
    {
        printf("请输入要查看的学生姓名：");
        char name[20];
        scanf("%19s", name);
        int count = getStuCountByName(stu_head, name);
        if (count != 0)
        {
            printf("\n有%d个姓名为%s的学生,如下:\n", count, name);
            print_Lines2();
            printf("学号\t姓名\t班级\t年龄\t性别\n");
            print_Lines2();
            print_StudentNodeByName(stu_head, name);
            return_Main();
        }
        else
        {
            printf("没有姓名为%s的学生!\n", name);
            return_function();
        }
    }
    else
    {
        printf("输入错误!\n");
    }
}

// 查看所有学生信息
void show_all(Student_Node_p stu_head)
{
    const int pageSize = 5; // 每页显示5条记录
    int total = stuCount(stu_head);
    int totalPages = (total + pageSize - 1) / pageSize; // 计算总页数
    int currPage = 1;
    Student_Node_p currNode = stu_head->next;

    do
    {
        printf("          全部学生信息(第%d页/共%d页)\n", currPage, totalPages);
        print_Lines();
        printf("共有%d 个学生信息,信息如下:\n", stuCount(stu_head));
        print_Lines2();
        printf("学号\t姓名\t班级\t年龄\t性别\n");
        print_Lines2();
        int count = 0;
        Student_Node_p pageStart = currNode; // 保存当前页起始节点
        while (currNode != NULL && count < pageSize)
        {
            printf("%s\t%s\t%s\t%d\t%s\n", currNode->data->id, currNode->data->name, currNode->data->class, currNode->data->age, getSex(currNode->data->sex));
            count++;
            currNode = currNode->next;
        }
        print_Lines();
        printf("指令: (A)上一页 (D)下一页 (Q)退出: ");

        // 翻页
        while (1)
        {
            char input;
            scanf(" %c", &input);
            while (getchar() != '\n')
                ; // 清空输入缓冲区
            if (input == 'a' || input == 'A')
            {
                if (currPage > 1)
                {
                    Student_Node_p newStart = pageStart;
                    for (int i = 0; i < pageSize && newStart->prev != NULL; i++)
                    {
                        newStart = newStart->prev;
                    }
                    if (newStart != pageStart)
                    { // 确认指针已移动
                        currNode = newStart;
                        currPage--;
                        clearScreen();
                        break;
                    }
                }
                else
                {
                    printf("已经是第一页了,请按D移到下一页:");
                }
            }
            else if (input == 'd' || input == 'D')
            {
                if (currPage < totalPages)
                {
                    if (currNode != NULL)
                    { // 确保没有越界
                        currPage++;
                        clearScreen();
                        break;
                    }
                }
                else
                {
                    printf("已经是最后一页了,请按A移到上一页:");
                }
            }
            else if (input == 'q' || input == 'Q')
            {
                return;
            }
            else
            {
                printf("输入错误,请重新输入:");
            }
        }

    } while (1);
}

// 查看班级学生信息
void show_class(Student_Node_p stu_head)
{
    printf("               查看班级学生信息                 \n");
    print_Lines();
    printf("请输入要查询的班级:");
    char class[20];
    scanf("%19s", class);
    clearScreen();

    const int pageSize = 5; // 每页显示5条记录
    int total = getStuCountByClass(stu_head, class);
    int totalPages = (total + pageSize - 1) / pageSize; // 计算总页数
    int currPage = 1;

    if (total == 0)
    {
        printf("未找到%s班的学生记录\n", class);
        if (return_function() == 1)
        {
            show_class(stu_head);
        }
        return;
    }

    Student_Node_p currNode = stu_head->next;
    do
    {
        printf("         %s班学生信息(第%d页/共%d页)\n", class, currPage, totalPages);
        print_Lines();
        printf("共有%d 个学生信息,信息如下:\n", total);
        print_Lines2();
        printf("学号\t姓名\t年龄\t性别\n");
        print_Lines2();
        int count = 0;
        Student_Node_p pageStart = currNode; // 保存当前页起始节点

        while (currNode != NULL && count < pageSize)
        {
            if (strcmp(currNode->data->class, class) == 0)
            {
                printf("%s\t%s\t%d\t%s\n", currNode->data->id, currNode->data->name, currNode->data->age, getSex(currNode->data->sex));
                count++;
            }

            currNode = currNode->next;
        }
        print_Lines();
        printf("指令: (A)上一页 (D)下一页 (Q)退出: ");

        // 翻页
        while (1)
        {
            char input;
            scanf(" %c", &input);
            while (getchar() != '\n')
                ; // 清空输入缓冲区
            if (input == 'a' || input == 'A')
            {
                if (currPage > 1)
                {
                    int remain = pageSize;
                    currNode = pageStart; // 从当前页第一节点回退
                    while (remain > 0 && currNode->prev != NULL)
                    {
                        currNode = currNode->prev;
                        if (strcmp(currNode->data->class, class) == 0)
                        {
                            remain--;
                        }
                    }
                    currPage--;
                    clearScreen();
                    break;
                }
                else
                {
                    printf("已经是第一页了,请按D移到下一页:");
                }
            }
            else if (input == 'd' || input == 'D')
            {
                if (currPage < totalPages)
                {
                    if (currNode != NULL)
                    { // 确保没有越界
                        currPage++;
                        clearScreen();
                        break;
                    }
                }
                else
                {
                    printf("已经是最后一页了,请按A移到上一页:");
                }
            }
            else if (input == 'q' || input == 'Q')
            {
                return;
            }
            else
            {
                printf("输入错误,请重新输入:");
            }
        }

    } while (1);
}

// 获取性别
char *getSex(int sex)
{
    if (sex == 1)
        return "男";
    else
        return "女";
}